package org.azmat.demoredis.Redis.RedisTemplate;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;

@SpringBootTest
public class RedisOpsForSet {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void testSet1() {
        /**
         * ● add(K k,V ... vs)方法：将数据(元素)添加到集合。
         * ● size(K k)方法：返回集合的长度，及集合中的元素的个数。
         * ● Set(V) menbers(K k)：返回集合中的所有元素。
         * ● remove(K k,Object .... objects)：删除元素。
         * ● pop(K k)：随机删除。
         */
        redisTemplate.opsForSet().add("set1", "a", "b", "c", "d");
        System.out.println(redisTemplate.opsForSet().size("set1"));
        System.out.println(redisTemplate.opsForSet().members("set1"));
        redisTemplate.opsForSet().remove("set1", "a", "b");
        System.out.println(redisTemplate.opsForSet().members("set1"));
        redisTemplate.opsForSet().pop("set1");
        System.out.println(redisTemplate.opsForSet().members("set1"));
    }

    @Test
    void testSet2() {
        /**
         * Boolean move(K k,V v,K k1)方法：将元素移动到另一个集合。
         */
        String[] moveList = {"a", "b", "c", "d"};
        String[] moveList1 = {"e", "f", "g", "h"};
        redisTemplate.opsForSet().add("set1", moveList);
        redisTemplate.opsForSet().add("set2", moveList1);
        System.out.println(redisTemplate.opsForSet().members("set1"));
        System.out.println(redisTemplate.opsForSet().members("set2"));
        System.out.println(redisTemplate.opsForSet().move("set1", "a", "set2"));
        System.out.println(redisTemplate.opsForSet().members("set1"));
        System.out.println(redisTemplate.opsForSet().members("set2"));
    }

    @Test
    void testSet3() {
        /**
         * Cursor<V> scan (K k,ScanOptions scanOptions)：遍历集合中的所有元素。
         */
        redisTemplate.opsForSet().add("set1", "a", "b", "c", "d");
        Cursor<Object> cursor = redisTemplate.opsForSet().scan("set1", ScanOptions.NONE);
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    }
}
